<template>
  <div>
    <transition name="van-fade">
      <router-view class="main"></router-view>
    </transition>
  </div>
</template>

<script>
import { apiLogging } from './api/index'
export default {
  created() {
    // 检查用户是否登录
    if (localStorage.getItem('logging')) {
      const id = JSON.parse(localStorage.getItem('logging')).id || undefined
      if (id) {
        apiLogging(id)
        .then(res => {
              this.$store.state.user = res.data.data.result
              this.$store.state.logging = 1 
          })
          .catch(err => {
              console.log(err)
          })
      }
    }

    // 判断tabbar选中
    switch (this.$route.path) {
      case '/home':
        this.$store.state.active = 0
        break;
      case '/search':
        this.$store.state.active = 1
        break;
      case '/order':
        this.$store.state.active = 2
        break;
      case '/user':
        this.$store.state.active = 3
        break;
    }
  },
  watch: {
    // 监听tabbar
    '$route.path'(newVal) {
      switch (newVal) {
        case '/home':
          this.$store.state.active = 0
          break;
        case '/search':
          this.$store.state.active = 1
          break;
        case '/order':
          this.$store.state.active = 2
          break;
        case '/user':
          this.$store.state.active = 3
          break;
      }
    }
  },
}
</script>

<style lang="less"> 
  p {
    margin: 0;
  }
  .main {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
  }
  // update
  .update {
      margin-top: 30px;
    .save {
        margin-top: 20px;
    }
  }
</style>